package com.shetou.yun.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.shetou.yun.R;
import com.shetou.yun.utils.CameraSurfaceView;
import com.shetou.yun.utils.MyEvent;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class PhotoActivity extends AppCompatActivity {

    @BindView(R.id.imageView1)
    ImageView imageView1;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.imageView3)
    ImageView imageView3;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.take_photo)
    Button button2;
    @BindView(R.id.imageView4)
    ImageView imageView4;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.textView4)
    TextView textView4;
    @BindView(R.id.textView5)
    TextView textView5;
    @BindView(R.id.next)
    Button next;
    CameraSurfaceView cameraSurfaceView;
    int im1 = 0;
    int im2 = 0;
    int im3 = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        cameraSurfaceView = (CameraSurfaceView) findViewById(R.id.cameraSurfaceView);
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageView1.setImageResource(R.drawable.shetouphoto);
                im1 = 0;
                onepath = null;
            }
        });
        imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageView2.setImageResource(R.drawable.shetouphoto);
                im2 = 0;
                twopath = null;
            }
        });
        imageView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageView3.setImageResource(R.drawable.shetouphoto);
                im3 = 0;
                threepath = null;
            }
        });
    }

    String onepath=null;
    String twopath=null;
    String threepath=null;

    public void onEventMainThread(MyEvent event) {

        if (!TextUtils.isEmpty(event.getType())) {
            if (event.getType().equals("A1")) {

                String content = event.getContent();
                Log.e("文件", "文件路劲" + content);
                if (im1 == 0) {
                    Glide.with(this).load(content).into(imageView1);
                    im1 = 1;
                    onepath = content;
                    return;
                }
                if (im2 == 0) {

                    Glide.with(this).load(content).into(imageView2);
                    im2 = 1;
                    twopath = content;
                    return;
                }
                if (im3 == 0) {

                    Glide.with(this).load(content).into(imageView3);
                    im3 = 1;
                    threepath = content;
                    return;

                }
            }


        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cameraSurfaceView.closeCamera();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.take_photo, R.id.next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.take_photo:
                cameraSurfaceView.capture();
                break;
            case R.id.next:
                if (onepath == null || twopath == null || threepath == null) {
                    Toast.makeText(this, "请拍摄3张照片！", Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(this, "图片路径1：" + onepath + "图片路径2：" + twopath + "图片路径3：" + threepath, Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, LoadActivity.class));
                break;
        }
    }
}
